package collection.斗地主分牌;

import java.util.*;

/**
 * 功能：做牌  洗牌  定义三个玩家  发牌 排序  看牌
 */
public class GameDemo {
    /**
     * 1.定义一个静态集合存储54张牌对象
     */
    public static List<Card> allCard = new ArrayList<>();

    /**
     * 2.做牌：定义静态代码块初始化数据
     */
    static {
        //3.定义点数：个数类型，使用数组
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //4.定义花色
        String[] colors = {"♥", "♦", "♣", "♠"};
        //5.组合点数和花色
        int index = 0;
        for (String size : sizes) {
            index++;
            for (String color : colors) {
                //6.封装成一个对象
                Card c = new Card(size, color, index);
                //6.存入到集合容器中去
                allCard.add(c);
            }
        }
        //8.把大小王存入集合中去
        Card c1 = new Card("", "🃏", ++index);
        Card c2 = new Card("", "🤡", ++index);
        Collections.addAll(allCard, c1, c2);
        System.out.println("新牌：" + allCard);
    }

    public static void main(String[] args) {
        //9.洗牌
        Collections.shuffle(allCard);
        System.out.println("洗牌后：" + allCard);
        //10.发牌，定义三个玩家，每个玩家的牌也是集合容器
        List<Card> xiaoyu = new ArrayList<>();
        List<Card> chenglong = new ArrayList<>();
        List<Card> laodie = new ArrayList<>();
        //11.开始发牌 （从牌集合中发51张牌给三个玩家，最后三张是底牌）
        for (int i = 0; i < allCard.size() - 3; i++) {
            //先拿到当前牌对象
            Card c = allCard.get(i);
            if (i % 3 == 0) {
                xiaoyu.add(c);
            } else if (i % 3 == 1) {
                chenglong.add(c);
            } else if (i % 3 == 2) {
                laodie.add(c);
            }
        }
        //12.拿最后三张底牌
        List<Card> last3cards = allCard.subList(allCard.size() - 3, allCard.size());
        //13.给玩家把牌排好序
        sortCards(xiaoyu);
        sortCards(chenglong);
        sortCards(laodie);

        //14.输出玩家的牌
        System.out.println("小玉" + xiaoyu);
        System.out.println("成龙" + chenglong);
        System.out.println("老爹" + laodie);
        System.out.println("三张底牌：" + last3cards);
    }

    /**
     * 给牌排序的方法
     *
     * @param cards
     */
    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getIndex() - o1.getIndex();
            }
        });
    }
}
